package com.zfy.cmsservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zfy.cmsservice.entity.Subjects;
import com.zfy.cmsservice.entity.excel.SubjectData;
import com.zfy.cmsservice.entity.subject.OneSubject;
import com.zfy.cmsservice.entity.subject.TwoSubject;
import com.zfy.cmsservice.listener.SubjectExcelLister;
import com.zfy.cmsservice.mapper.SubjectsMapper;
import com.zfy.cmsservice.service.SubjectsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 科目表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-11-06
 */
@Service
public class SubjectsServiceImpl extends ServiceImpl<SubjectsMapper, Subjects> implements SubjectsService {

    @Override
    public void saveSubject(MultipartFile file, SubjectsService subjectsService) {
        try{
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法读取
            EasyExcel.read(in, SubjectData.class,new SubjectExcelLister(subjectsService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类 parent_id = 0
        QueryWrapper<Subjects> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<Subjects> oneSubjectList = baseMapper.selectList(wrapperOne);

        //查询所有二级分类 parent_id ！= 0
        QueryWrapper<Subjects> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<Subjects> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建集合存储封装的数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到oneSubjectList每个eduSubject对象
            Subjects eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);

            //在一级分类遍历二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //获取每个二级分类
                Subjects tSubject = twoSubjectList.get(m);
                //判断二级分类的parent_id和一级一致
                if(tSubject.getParentId().equals(eduSubject.getId())){
                    //tSubject给TwoSubject
                    TwoSubject twoSubject = new TwoSubject();
//                    twoSubject.setTitle(tSubject.getTitle());
                    BeanUtils.copyProperties(tSubject,twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            //把一级下所有的二级放入一级
            oneSubject.setChildren(twoFinalSubjectList);
        }
        //封装二级分类
        return finalSubjectList;
    }
}
